<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%
  String path = request.getContextPath();
%>
<jsp:include page="/pages/commons/top.jsp" />
<jsp:include page="/pages/commons/left.jsp" />

<div data-options="region:'center',title:'客户管理',iconCls:'icon-ok'">  
	<table id="dg" title="客户列表" class="easyui-datagrid" style="width: 1180px; height: 447px" toolbar="#toolbar" fitColumns="true" method="get" singleSelect="false" loadMsg="数据载入中...">
		<thead>
			<tr>
			    <th data-options="field:'ck',checkbox:true"></th> 
				<th field="id" width="50">流水号</th>
				<th field="unit" width="50">客户单位</th>
				<th field="name" width="50">客户名称</th>
				<th field="customerTypeName" width="50">客户类型</th>
				<th field="customerLevelName" width="50">客户等级</th>
				<th field="branch" width="50">院系</th>
				<th field="dept" width="50">部门</th>
				<th field="post" width="50">职位</th>
				<th field="phone" width="50">座机</th>
				<th field="mobile" width="50">手机</th>
				<th field="responsiblePerson" width="50">负责人</th>
				<th field="relationCreateTimeStr" width="50">关系建立时间</th>
				<th field="chargeScope" width="50">负责范围</th>
			</tr>
		</thead>
	</table>
	<div id="toolbar" style="padding:5px;height:auto">  
        <div style="margin-bottom:5px">  
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newCustomer()">添加客户</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editCustomer()">修改客户</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delCustomer()">删除客户</a>
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="showCustomer()">查看客户</a>
		</div>
		<div> 
		    <label>客户名称：</label> <input id="queryNameObj" name="queryName" class="easyui-validatebox"/>
		    <label>客户等级：</label> <input id="queryCustomerLevelObj" name="queryCustomerLevel" url="<%=path%>/dictionary/getList.do?dictType=CUSTOMER_LEVEL" class="easyui-combobox" valueField="dictValue" textField="dictName"/>
		    <label>客户单位：</label> <input id="queryUnitObj" name="queryUnit" class="easyui-validatebox"/>
		    <label>院系：</label> <input id="queryBranchObj" name="queryBranch" class="easyui-validatebox"/> 
		    <label>负责人：</label><input id="queryResponsiblePersonObj" name="queryResponsiblePerson" class="easyui-combobox" valueField="loginName" textField="userName"/> 
            <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="queryCustomer()">查询</a>  
        </div>
	</div>
	<div id="page" class="easyui-pagination" style="border:1px solid #ddd;width: 1180px"></div>

	<div id="dlg" class="easyui-dialog" style="width: 1000px; height: 500px; padding: 10px 20px" closed="true" buttons="#dlg-buttons">
		<div class="ftitle">客户信息</div>
		<form id="fm" method="post" novalidate>
		    <input id="idObj" name="id" type="hidden"/>
		    <div class="fitem">
				<label>客户名称:</label><input id="nameObj" name="name" class="easyui-validatebox" required="true" validType="length[0,50]"/>
				<label>职位:</label><input id="postObj" name="post" class="easyui-validatebox" validType="length[0,50]"/>
				<label>客户类型:</label><input id="customerTypeObj" name="customerType" class="easyui-combobox" url="<%=path%>/dictionary/getList.do?dictType=CUSTOMER_TYPE" required="true" valueField="dictValue" textField="dictName"/>
				<label>客户等级:</label><input id="customerLevelObj" name="customerLevel" class="easyui-combobox" url="<%=path%>/dictionary/getList.do?dictType=CUSTOMER_LEVEL" required="true" valueField="dictValue" textField="dictName"/>
			</div>
			<div class="fitem">
				<label>院系:</label><input id="branchObj" name="branch" class="easyui-validatebox" validType="length[0,100]"/>
				<label>部门:</label><input id="deptObj" name="dept" class="easyui-validatebox" validType="length[0,100]"/>
				<label>负责人:</label><input id="responsiblePersonObj" name="responsiblePerson" class="easyui-combobox" valueField="loginName" textField="userName"/>
				<label>关系建立时间:</label><input id="relationCreateTimeObj" name="relationCreateTimeStr" class="easyui-datebox"/>
				
			</div>
			<div class="fitem">
				<label>座机:</label><input id="phoneObj" name="phone" class="easyui-validatebox" validType="length[0,30]"/>
				<label>手机:</label><input id="mobileObj" name="mobile" class="easyui-validatebox" validType="length[0,30]"/>
				<label>Email:</label><input id="emailObj" name="email" class="easyui-validatebox" validType="email"/>
			</div>
			<div class="fitem">
			    <label>创建时间:</label><input id="createTimeObj" name="createTimeStr" class="easyui-validatebox" disabled="true"/>
				<label>修改时间:</label><input id="modifyTimeObj" name="modifyTimeStr" class="easyui-validatebox" disabled="true"/>
			</div>
			
			<div class="fitem">
				<label>客户单位:</label><input id="unitObj" style="width:770px" class="easyui-validatebox" name="unit" required="true" validType="length[0,200]"/>
			</div>
			<div class="fitem">
				<label>办公地址:</label><input id="workSiteObj" style="width:770px" name="workSite" class="easyui-validatebox" validType="length[0,500]"/>
			</div>
			<div class="fitem">
				<label>家庭住址:</label><input id="homeAddressObj" style="width:770px" name="homeAddress" class="easyui-validatebox" validType="length[0,500]"/>
			</div>
			<div class="fitem">
				<label>送货地址:</label><input id="deliveryAddressObj" style="width:770px" name="deliveryAddress" class="easyui-validatebox" validType="length[0,500]"/>
			</div>
			<div class="fitem">
			    <label>兴趣爱好:</label><input id="interestObj" style="width:770px"  name="interest" class="easyui-validatebox" validType="length[0,500]"/>
			</div>
			<div class="fitem">
			    <label>负责范围:</label><input id="chargeScopeObj" style="width:770px" name="chargeScope" class="easyui-validatebox" validType="length[0,500]"/>
			</div>
		</form>
	</div>
	<div id="dlg-buttons">
		<a id="saveButton" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveCustomer()">保存</a> 
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
	</div>
</div>

<jsp:include page="/pages/commons/footer.jsp" />


<script type="text/javascript">  
        var url;  
        function newCustomer(){  
            $('#dlg').dialog('open').dialog('setTitle','添加客户'); 
            $('#saveButton').linkbutton('enable'); 
            $('#fm').form('clear'); 
            url = "<%=path%>/customer/add.do";  
            addOption(null);
        } 

        function addOption(row){
			if(row != null){
			    $("#customerTypeObj").combobox("select",row.customerType);
			    $("#customerLevelObj").combobox("select",row.customerLevel);
			}
        }		
        

        function editCustomer(){  
            var row = $('#dg').datagrid('getSelected');  
            $('#saveButton').linkbutton('enable'); 
            if (row){  
                $('#dlg').dialog('open').dialog('setTitle','修改客户');  
                $('#fm').form('load',row);
                url = "<%=path%>/customer/mod.do";  
                addOption(row);
            }  
        } 
        
        function showCustomer(){
        	var row = $('#dg').datagrid('getSelected');  
            if (row){
            	$('#dlg').dialog('open').dialog('setTitle','客户信息');  
                $('#fm').form('load',row);
                $('#saveButton').linkbutton('disable'); 
            }
        }
        
        function saveCustomer(){ 
			if($('#fm').form('validate')){
				var data = "";
				data += "name="+$("#nameObj").val();
			    data += "&customerLevel="+$("#customerLevelObj").combobox("getValue");
				data += "&customerType="+$("#customerTypeObj").combobox("getValue");
				
				if($("#idObj").val()!=''){
				    data += "&id="+$("#idObj").val();
				}
				if($("#postObj").val()!=''){
					data += "&post="+$("#postObj").val();
				}
				if($("#unitObj").val()!=''){
					data += "&unit="+$("#unitObj").val();
				}
                if($("#branchObj").val()!=''){
                	data += "&branch="+$("#branchObj").val();
				}
                if($("#deptObj").val()!=''){
                	data += "&dept="+$("#deptObj").val();
				}
                if($("#workSiteObj").val()!=''){
                	data += "&workSite="+$("#workSiteObj").val();
				}
                if($("#deliveryAddressObj").val()!=''){
                	data += "&deliveryAddress="+$("#deliveryAddressObj").val();
				}
                if($("#phoneObj").val()!=''){
                	data += "&phone="+$("#phoneObj").val();
				}
                if($("#mobileObj").val()!=''){
                	data += "&mobile="+$("#mobileObj").val();
				}
                if($("#relationCreateTimeObj").datebox('getValue') !=''){
                	data += "&relationCreateTimeStr="+$("#relationCreateTimeObj").datebox('getValue')+" 00:00:00";
				}
                if($("#interestObj").val()!=''){
                	data += "&interest="+$("#interestObj").val();
				}
                if($("#chargeScopeObj").val()!=''){
                	data += "&chargeScope="+$("#chargeScopeObj").val();
				}
                if($("#responsiblePersonObj").combobox('getValue')!=''){
                	data += "&responsiblePerson="+$("#responsiblePersonObj").combobox('getValue');
				}
                if($("#homeAddressObj").val()!=''){
                	data += "&homeAddress="+$("#homeAddressObj").val();
				}
                if($("#emailObj").val()!=''){
                	data += "&email="+$("#emailObj").val();
				}
			    $.post(url,data,saveCallBack);  
			}
        } 

        function saveCallBack(result){
            var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
            }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dlg').dialog('close');      // close the dialog  
                $('#dg').datagrid('reload');
				initPage("");
    	        return true;
            } 
        }	
 
        function delCustomer(){  
            var row = $('#dg').datagrid('getSelections');  
            if (row.length > 0){  
                $.messager.confirm('Confirm','确定删除该客户吗?',function(r){  
                    if (r){  
						var data = "";
						for(var i=0;i<row.length;i++){
						    data += row[i].id
						    if(i<row.length - 1){
						        data+=",";
							}
						}
                    	$.post("<%=path%>/customer/del.do",{'ids':data},delCallBack);   
                    }  
                });  
            }  
        } 
        
        function delCallBack(result){
        	var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
	        }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dg').datagrid('reload');
	            var data = "?name="+$("#queryNameObj").val();
	        	data += "&customerLevel="+$("#queryCustomerLevelObj").combobox('getValue');
	        	data += "&unit="+$("#queryUnitObj").val();
	        	data += "&branch="+$("#queryBranchObj").val();
	        	data += "&responsiblePerson="+$("#queryResponsiblePersonObj").combobox('getValue');
				initPage(data);
    	        return true;
            } 
        } 
		
		$(function(){ 
　　                              initPage("");
           initDataGrid();
　　                   }); 

        function initDataGrid(){
		    var pageSize = $('#page').pagination('options').pageSize;
			var pageNum = $('#page').pagination('options').pageNumber;
			var startNum = pageSize*(pageNum-1);
			var queryData = "?startNum="+startNum+"&limit="+pageSize;
		    var queryUrl = "<%=path %>/customer/getListForPage.do"+queryData;
        	$('#dg').datagrid({url:queryUrl});
		}
        
        function initPage(data){
        	$.post("<%=path%>/customer/getTotalCount.do"+data,null,function(result){
        		$('#page').pagination({total:result});
            });
        }
        
        
        function queryCustomer(){
        	var data = "?name="+$("#queryNameObj").val();
        	data += "&customerLevel="+$("#queryCustomerLevelObj").combobox('getValue');
        	data += "&unit="+$("#queryUnitObj").val();
        	data += "&branch="+$("#queryBranchObj").val();
        	data += "&responsiblePerson="+$("#queryResponsiblePersonObj").combobox('getValue');
			initPage(data);
			var pageSize = $('#page').pagination('options').pageSize;
			var pageNum = $('#page').pagination('options').pageNumber;
			var startNum = pageSize*(pageNum-1);
        	var queryData = data + "&startNum="+startNum+"&limit="+pageSize;
        	var queryUrl = "<%=path %>/customer/getListForPage.do"+queryData;
        	$('#dg').datagrid({url:queryUrl});
        }
        
        
        $('#page').pagination({
        	onSelectPage:function(pageNumber, pageSize){
        		queryCustomer();
        	},
        	onRefresh:function(pageNumber, pageSize){
        		queryCustomer();
        	},
        	onChangePageSize:function(pageSize){
        		queryCustomer();
        	}
        });

    </script>
<style type="text/css">
#fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}
</style>

</html>
